package mamikeji.liaowa.ui.activity.notice;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.EventDetailsBean;
import mamikeji.liaowa.bean.EventListBean;
import mamikeji.liaowa.http_api.ForHttp;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.ui.activity.BaseActivity;
import mamikeji.liaowa.ui.activity.right.EventDetailsActivity;
import mamikeji.liaowa.ui.adapter.NoticeEventAdapter;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForLog;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;

public class Notice_EventActivity extends BaseActivity {
    public static final String TAG = "Notice_EventActivity";
    private ListView listView_notice_event;
    private NoticeEventAdapter eventListAdapter;
    List<EventListBean.DataEntity> dataEntityList = new ArrayList<>();
    private EventListBean eventListBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice_event);
        getBase();
        setTitleLeftBtn();
        setTitleName(R.string.notice_event_name);

        listView_notice_event = (ListView) findViewById(R.id.listView_notice_event);
        eventListAdapter = new NoticeEventAdapter(dataEntityList, this);
        eventListAdapter.bool = true;
        listView_notice_event.setAdapter(eventListAdapter);

        getEventList();

        listView_notice_event.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                EventDetailsBean eventDetailsBean = new EventDetailsBean(
                        eventListBean.getData().get(i).getImgurl(),
                        eventListBean.getData().get(i).getJumpurl(),
                        eventListBean.getData().get(i).getPublishdate(),
                        eventListBean.getData().get(i).getId(),
                        eventListBean.getData().get(i).getJumpurl(),
                        eventListBean.getData().get(i).getTitle(),
                        eventListBean.getData().get(i).getImgtype(),
                        eventListBean.getData().get(i).getContent()
                );
                Intent intent = new Intent(Notice_EventActivity.this, EventDetailsActivity.class);
                intent.putExtra("ActivityBean", eventDetailsBean);
                startActivity(intent);
            }
        });
    }

    public void getEventList() {
        showLoading();
        //参数集合
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put(HttpKey.EVENT_MY_USERCODE, ForPreference.getString(ForContents.USER_CODE));
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.GET_ACTIVITY, formMap, new StringCallback() {

            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG, e.getMessage());
                ForToast.showToast(R.string.intent_error);
                hideLoading();
            }

            @Override
            public void onResponse(String response) {
                ForLog.i(TAG, response);
                hideLoading();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        eventListBean = new Gson().fromJson(response, EventListBean.class);
                        if (eventListBean.getData() != null) {
                            dataEntityList = eventListBean.getData();
                            eventListAdapter.setList(dataEntityList);
                        }
                    } else {
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
